package com.vandzi.android.androidcode.model;

import java.io.InputStream;
import java.net.URL;

import android.graphics.drawable.Drawable;
import android.net.Uri;

public class Application {

	/**
	 * Application name from element:
	 * 
	 * <h2 class="title"><a
	 * href="http://www.androidcode.sk/aplikacia/APPLICATION NAME"
	 * data-type="name">APPLICATION NAME</a></h2>
	 */
	private String name;
	/**
	 * Author name from element:
	 * 
	 * <div class="author" data-type="author">Autor: <span>AUTHOR
	 * NAME</span></div>
	 */
	private String author;
	/**
	 * URL of application image from element:
	 * 
	 * <img width="96" height="96" src=
	 * "http://www.androidcode.sk/wp-content/uploads/2011/10/IMAGE.png" />
	 * 
	 */
	private String linkToImage;
	/**
	 * URL to application details from element:
	 * 
	 * <h2 class="title"><a
	 * href="http://www.androidcode.sk/aplikacia/APPLICATION NAME"
	 * data-type="name">APPLICATION NAME</a></h2>
	 */
	private String linktToDetail;
	/**
	 * Average rating from element:
	 * 
	 * <span class="hidden" data-type="average">000.0</span>
	 */
	private String averageRating;

	/**
	 * Score from element:
	 * 
	 * <span class="hidden" data-type="score">000.0</span>
	 */
	private String score;
	/**
	 * Market link from element:
	 * 
	 * <span class="hidden" data-type="market_link"></span>
	 */
	private String marketLink;

	/**
	 * Application image
	 */
	private Drawable image;

	/**
	 * Details about applications- retrieved from @linkToDetail
	 */
	private ApplicationDetails applicationDetails;
	/**
	 * order in which is application loaded from website
	 */
	int order;

	public Application(String name, String author, String linkToImage,
			String linktToDetail, String averageRating, String marketLink,
			String score, int order) {
		super();
		this.name = name;
		this.author = author;
		this.linkToImage = linkToImage;
		this.linktToDetail = linktToDetail;
		this.averageRating = averageRating;
		this.marketLink = marketLink;
		this.image = loadImageFromWebOperations(linkToImage);
		this.score = score;
		this.order = order;
	}

	public String getName() {
		return name;
	}

	public String getAuthor() {
		return author;
	}

	public Float getAverageRating() {
		return Float.parseFloat(averageRating);
	}

	public Float getScore() {
		return Float.parseFloat(score);
	}

	public Drawable getImage() {
		return image;
	}

	public String getLinktToDetail() {
		return linktToDetail;
	}

	public String getMarketLink() {
		return marketLink;
	}

	public ApplicationDetails getApplicationDetails() {
		return applicationDetails;
	}

	public int getOrder() {
		return order;
	}

	private Drawable loadImageFromWebOperations(String url) {
		try {

			url = Uri.encode(url, ":/");

			InputStream is = (InputStream) new URL(url).getContent();
			Drawable d = Drawable.createFromStream(is, "src name");
			return d;
		} catch (Exception e) {
			System.out.println("Exc=" + e);
			return null;
		}
	}

	public void setApplicationDetails(ApplicationDetails applicationDetails) {
		this.applicationDetails = applicationDetails;
	}

	public String getPackageName() {
		String packageName = null;
		packageName = this.marketLink.replace(
				"https://market.android.com/details?id=", "");

		return packageName;

	}

}
